#include "boxserver.h"
#include "boxthread.h"

#include <QtNetwork>
#include "nam.h"
#include "nbr.h"

BoxServer::BoxServer(QObject *parent)
    : QTcpServer(parent)
{
    qDebug("BoxServer::BoxServer() \n");
    m_thread_count = 0;
}

BoxServer::~BoxServer()
{
    qDebug("BoxServer::~BoxServer() \n");
}

void BoxServer::incomingConnection(int socketDescriptor)
{
    qDebug("BoxServer::incomingConnection(): %d \n", socketDescriptor);
    BoxThread *thread = new BoxThread(socketDescriptor, this, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    connect(thread, SIGNAL(finished()), this, SLOT(on_thread_finish()));
    m_thread_count++;
    qDebug() << "TH-START" << m_thread_count;
    thread->start();
}

void BoxServer::on_thread_finish()
{
    m_thread_count--;
    qDebug() << "TH-FINISH" << m_thread_count;
}
